import { _decorator, Component, Node, Label } from 'cc';
const { ccclass, property } = _decorator;

//倒计时显示等级
export enum BCSecCountdownLabelLevel{
	SEC = 0,							//00
	MIN,								//00:00
	HOUR,								//00:00:00
	DAY,								//00 00:00:00
	MONTH,								//00-00 00:00:00
	YEAR								//00-00-00 00:00:00
};

@ccclass('BCSecCountdownLabel')
export class BCSecCountdownLabel extends Label {
	private m_endTime: number = 0;
	private m_finishCallBack: Function = null;   // 结束回调

	_startWithLeftTime(leftTime: number, ext?: string, cb?: Function) {
		this.m_endTime = leftTime + new Date().getTime() / 1000;
		cb && (this.m_finishCallBack = cb);

		this.schedule(this._refreshLable, 1)
		this._refreshLable(ext);
	}

	private _refreshLable(ext?: string) {
		let curLeftTime = this.m_endTime - new Date().getTime() / 1000;
		curLeftTime = Math.round(curLeftTime);
		if (curLeftTime < 0) {
			curLeftTime = 0;
		}
		this.string = curLeftTime.toString() + ext;

		if (curLeftTime === 0) {
			this.unschedule(this._refreshLable);
			this.m_finishCallBack && this.m_finishCallBack()
		}
	}
}

